package com.wdzg.member.generate.freemaker.generator.element;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.wdzg.member.generate.freemaker.generator.BaseGenerator;
import com.wdzg.member.generate.freemaker.generator.GeneratorHtml;
import com.wdzg.member.generate.freemaker.generator.bean.Column;
import com.wdzg.member.generate.freemaker.generator.bean.XmlBean;

public class JsGenerator extends BaseGenerator {

	public JsGenerator(XmlBean bean) {
		super(bean);
	}

	@Override
	public boolean generatorHtml(String ftlPath) {
		try {
			ftlFile = new File(JsGenerator.class.getResource("/").getPath() + ftlPath);
			
			HashMap<String, Object> contextMap = new HashMap<String, Object>();
			contextMap.put("modelName", bean.modelName);
			contextMap.put("tableName", bean.tableName);
			contextMap.put("className", StringUtils.uncapitalize(bean.className));
			contextMap.put("saveUrl", getPathWithEnd(bean.path.visitUrl) +"save");
			contextMap.put("deleteUrl", getPathWithEnd(bean.path.visitUrl) +"delete");
			contextMap.put("updateUrl", getPathWithEnd(bean.path.visitUrl) +"update");
			contextMap.put("visitUrl", bean.path.visitUrl);
			//contextMap.put("searchList", getSearchList());
			//contextMap.put("saveList", getSaveList());
			contextMap.put("updateList", getUpdateList());
			contextMap.put("viewList", getViewList());
			
			GeneratorHtml generator = new GeneratorHtml();
			generator.geneHtmlFile("js.ftl", ftlFile.getPath(), contextMap, getPathWithEnd(bean.path.js), bean.modelName +".js");
			
			return true;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return false;
	}

	/**
	 * 查询条件列表
	 * 
	 * @return
	 */
	public List<Column> getSearchList() {
		List<Column> list = bean.columns;
		List<Column> searchList = new ArrayList<Column>();
		for (Column col : list) {
			if (col.getSearch()) {
				searchList.add(col);
			}
		}
		
		return searchList;
	}
	
	/**
	 * 保存列表
	 * 
	 * @return
	 */
	public List<Column> getSaveList() {
		List<Column> list = bean.columns;
		List<Column> saveList = new ArrayList<Column>();
		for (Column col : list) {
			if (col.getSave()) {
				saveList.add(col);
			}
		}
		
		return saveList;
	}
	
	/**
	 * 更新列表
	 * 
	 * @return
	 */
	public List<Column> getUpdateList() {
		List<Column> list = bean.columns;
		List<Column> updateList = new ArrayList<Column>();
		for (Column col : list) {
			if (col.getUpdate()) {
				updateList.add(col);
			}
		}
		
		return updateList;
	}
	
	/**
	 * 查看列表
	 * 
	 * @return
	 */
	public List<Column> getViewList() {
		List<Column> list = bean.columns;
		List<Column> viewList = new ArrayList<Column>();
		for (Column col : list) {
			if (col.getView()) {
				viewList.add(col);
			}
		}
		
		return viewList;
	}
}
